package com.huanzhidadi.java8inaction.chapter8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Point {

    public final static Comparator<Point> compareByXandThenY =
            Comparator.comparing(Point::getX)
                    .thenComparing(Point::getY);

    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public boolean equals(Object obj) {
        Point target = (Point) obj;
        return this.getX() == target.getX()
                && this.getY() == target.getY();
    }

    public Point moveRightBy(int x) {
        return new Point(this.x + x, this.y);
    }

    public static List<Point> moveAllPointsRightBy(List<Point> points, int x) {
        return points.stream()
                .map(p -> new Point(p.getX() + x, p.getY()))
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        List<Point> points = Arrays.asList(new Point(12, 2), null);
        points.stream()
                // .map(point -> point.getX())
                .map(Point::getX)
                .forEach(System.out::println);
    }
}
